package com.education.wisdom.controller.app;//package com.ruoyi.web.controller.app;

import com.education.common.core.BaseController;
import com.education.common.utils.LogUtil;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.pojo.Bbs;
import com.education.wisdom.pojo.ThumbsUp;
import com.education.wisdom.service.IBbsService;
import com.education.wisdom.service.IThumbsUpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
  * 点赞记录 信息操作处理
  *
  * @author ruoyi
  * @date 2018-11-12
  */
@Controller
@CrossOrigin
@Slf4j
@Api(tags = {"点赞记录 信息操作处理"})
@RequestMapping("/appThumbsUp")
public class AppThumbsUpController extends BaseController
{

	@Autowired
	private IThumbsUpService thumbsUpService;
	@Autowired
	private IBbsService bbsService;


	/**
	 * 新增保存点赞记录
	 */
	@ApiOperation("新增点赞记录")
	@PostMapping("/addSave")
	@ResponseBody
	@ApiImplicitParams({
		@ApiImplicitParam(name="dzUserloginid",value="点赞人编号",required=true,dataType="String"),
		@ApiImplicitParam(name="dzLtid",value="帖子编号",required=true,dataType="String")
	})
	public void addSave(HttpServletResponse response, HttpServletRequest request, ThumbsUp thumbsUp)throws Exception{
		log.info(LogUtil.LogInfo("新增点赞记录",new Date()));
			//获取到当前登陆人的id
			String userId = thumbsUp.getDzUserloginid();
			String dzUserloginid = userId;
			//职业达人视频编号
        	String ltId = thumbsUp.getDzLtid();
        	Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
			map.put("ltId", ltId);
			//根据当前登录人id和职业大人视频id查询点赞记录
			ThumbsUp ThumbsUp1 = thumbsUpService.selectIsthumbsUp(map);
			if (ThumbsUp1 != null ) {
				if ( "1".equals(ThumbsUp1.getDelFlag())) {
					Bbs bbs = bbsService.selectBbsById(Integer.parseInt(ltId));
		        	BigDecimal ltThumbsup = bbs.getLtThumbsup().add(new BigDecimal(1));
		        	bbs.setLtThumbsup (ltThumbsup);
		        	bbsService.updateBbs(bbs);
		        	thumbsUpService.updateThumbsUps(map);
					BaseController.writeJSON(ltThumbsup, response, "200", "点赞成功");
				}else {
					Map<String, Object> map1 = new HashMap<String, Object>();
					map1.put("dzUserloginid", dzUserloginid);
					map1.put("ltId", ltId);
					Bbs bbs = bbsService.selectBbsById(Integer.parseInt(ltId));
		        	BigDecimal ltThumbsup = bbs.getLtThumbsup().subtract(new BigDecimal(1));
		        	bbs.setLtThumbsup (ltThumbsup);
		        	bbsService.updateBbs(bbs);
		        	thumbsUpService.updateThumbsUp(map1);
					writeJSON(ltThumbsup, response, "200", "取消成功");
				}
			}else {
				thumbsUp.setDzId(UUIDUtils.getByUUId());
	        	thumbsUp.setDzThumbsuptime(new Date());
	        	Bbs bbs = bbsService.selectBbsById(Integer.parseInt(ltId));
	        	BigDecimal ltThumbsup = bbs.getLtThumbsup().add(new BigDecimal(1));
	        	bbs.setLtThumbsup (ltThumbsup);
	        	bbsService.updateBbs(bbs);
	        	thumbsUpService.insertThumbsUp(thumbsUp);
	    		writeJSON(ltThumbsup, response,  "200", "点赞成功");
			}
	}


	/**
	 * 修改保存点赞记录
	 */
	@ApiOperation("取消点赞")
	@PostMapping("/edit")
	@ResponseBody
	@ApiImplicitParams({
		@ApiImplicitParam(name="ltId",value="帖子编号",required=true,dataType="String"),
		@ApiImplicitParam(name="dzUserloginid",value="登录用户编号",required=true,dataType="String")
	})
	public void editSave(HttpServletResponse response, HttpServletRequest request,String ltId,String dzUserloginid) throws Exception {
		log.info(LogUtil.LogInfo("取消点赞", new Date()));
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("dzUserloginid", dzUserloginid);
			map.put("ltId", ltId);
			Bbs bbs = bbsService.selectBbsById(Integer.parseInt(ltId));
        	BigDecimal ltThumbsup = bbs.getLtThumbsup().subtract(new BigDecimal(1));
        	bbs.setLtThumbsup (ltThumbsup);
        	bbsService.updateBbs(bbs);
			writeJSON(thumbsUpService.updateThumbsUp(map), response, "200", "成功");
	}


}
